package com.oracle2mysql.migration.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import com.oracle2mysql.migration.Main;

import java.io.File;
import java.util.Properties;

/**
 * Maven插件类，用于在Maven构建过程中执行Oracle到MySQL的MyBatis迁移
 */
@Mojo(name = "migrate")
public class OracleToMysqlMigrationMojo extends AbstractMojo {
    
    /**
     * Maven项目对象
     */
    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    private MavenProject project;
    
    /**
     * 输入目录 - 存放需要转换的MyBatis XML文件
     */
    @Parameter(property = "inputDir", defaultValue = "${basedir}/src/main/resources/mapper")
    private File inputDir;
    
    /**
     * 输出目录 - 存放转换后的MyBatis XML文件
     */
    @Parameter(property = "outputDir", defaultValue = "${basedir}/target/generated-resources/mapper/mysql")
    private File outputDir;
    
    /**
     * 函数映射配置文件路径
     */
    @Parameter(property = "functionMappingFile")
    private File functionMappingFile;
    
    /**
     * 是否覆盖已存在的文件
     */
    @Parameter(property = "overwrite", defaultValue = "true")
    private boolean overwrite;
    
    /**
     * 是否生成HTML对比报告
     */
    @Parameter(property = "generateHtmlReport", defaultValue = "false")
    private boolean generateHtmlReport;
    
    /**
     * HTML报告输出路径
     */
    @Parameter(property = "htmlReportPath")
    private File htmlReportPath;
    
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("===== Oracle到MySQL的MyBatis迁移工具 - Maven插件开始执行 =====");
        
        try {
            // 设置系统属性，供Main类使用
            System.setProperty("input.dir", inputDir.getAbsolutePath());
            System.setProperty("output.dir", outputDir.getAbsolutePath());
            if (functionMappingFile != null && functionMappingFile.exists()) {
                System.setProperty("function.mapping.file", functionMappingFile.getAbsolutePath());
            }
            System.setProperty("overwrite", String.valueOf(overwrite));
            System.setProperty("generate.html.report", String.valueOf(generateHtmlReport));
            if (htmlReportPath != null) {
                System.setProperty("html.report.path", htmlReportPath.getAbsolutePath());
            }
            
            // 执行迁移
            Main.main(new String[]{});
            
            // 将生成的资源添加到项目中
            if (outputDir.exists()) {
                project.addResource(new org.apache.maven.model.Resource() {
                    {
                        setDirectory(outputDir.getAbsolutePath());
                    }
                });
            }
            
            getLog().info("迁移完成！");
            getLog().info("输入目录: " + inputDir.getAbsolutePath());
            getLog().info("输出目录: " + outputDir.getAbsolutePath());
            if (generateHtmlReport) {
                getLog().info("HTML报告生成: 已启用");
                if (htmlReportPath != null) {
                    getLog().info("HTML报告路径: " + htmlReportPath.getAbsolutePath());
                }
            } else {
                getLog().info("HTML报告生成: 已禁用");
            }
            
        } catch (Exception e) {
            getLog().error("迁移过程中发生错误: " + e.getMessage());
            throw new MojoExecutionException("迁移失败", e);
        }
    }
}